package com.android.launcher3.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.telephony.SubscriptionInfo;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.x.component.connectivity.ConnectivityComponent;
import com.x.component.connectivity.ConnectivityTool;
import com.x.component.connectivity.IconState;
import com.x.component.connectivity.MobileDataIndicators;
import com.x.component.connectivity.MobileState;
import com.x.component.connectivity.WifiIndicators;
import com.x.component.connectivity.WifiState;

import java.util.List;

public class TelephonyCellularWidget extends FrameLayout {

    private final String TAG = "TelephonyCellularWidget";
    private ImageView icon;
    private TextView tv_status;
    private Context mContext;
    private View container;

    private SwitchButton switchButton;

    public TelephonyCellularWidget(@NonNull Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public TelephonyCellularWidget(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    public TelephonyCellularWidget(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();
    }

    void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_telephony_cellular, this, true);
        container = findViewById(R.id.container);
        icon = findViewById(R.id.icon);
        tv_status = findViewById(R.id.tv_status);
        switchButton = findViewById(R.id.v_switch);

        switchButton.setListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton buttonView, boolean isChecked) {
                if (ConnectivityComponent.getInstance().isSimDetected()) {
                    Log.d("TinkerMobileData", "onCheckedChanged: " + isChecked);
                    ConnectivityComponent.getInstance().setMobileDataEnabled(isChecked, true);
                }
            }
        });
        ConnectivityComponent INSTANCE = ConnectivityComponent.getInstance();
        if (!INSTANCE.isSimDetected()) {
            icon.setImageLevel(0);
            tv_status.setText(mContext.getResources().getString(R.string.mobile_no_sim));
        } else {
            MobileState initMobileState = INSTANCE.getMobileState();
            String networkName = initMobileState.networkName;
            if (TextUtils.isEmpty(networkName)) {
                networkName = "";
            }
            tv_status.setText(networkName);
            icon.setImageLevel(initMobileState.level);
        }
        INSTANCE.setConnectivityTool(new ConnectivityTool() {
            @Override
            public void setWifiIndicators(WifiIndicators wifiIndicators) {
                Log.d(TAG, "setCallIndicator");
                WifiState wifiState = wifiIndicators.wifiState;
                if (wifiState != null) {
                    Log.d(TAG, "setWifiIndicators wifiState " + wifiState);
                }
            }

            @Override
            public void setMobileDataIndicators(MobileDataIndicators mobileDataIndicators) {
                Log.d(TAG, "setMobileDataIndicators");
                MobileState mobileState = mobileDataIndicators.mobileState;
                if (mobileState != null) {
                    Log.d(TAG, "setMobileDataIndicators mobileState " + mobileState);
                    icon.setImageLevel(mobileState.level);
                    if (INSTANCE.isSimDetected()) {
                        String networkName = mobileState.networkName;
                        if (TextUtils.isEmpty(networkName)) {
                            networkName = "";
                        }
                        tv_status.setText(networkName);
                    }
                }
            }

            @Override
            public void setSubs(List<SubscriptionInfo> list) {

            }

            @Override
            public void setNoSims(boolean show, boolean simDetected) {
                if (!simDetected) {
                    switchButton.setSelect(false);
                    switchButton.setClickable(false);
                    icon.setImageLevel(0);
                    tv_status.setText(mContext.getResources().getString(R.string.mobile_no_sim));
                }else{
                    switchButton.setClickable(true);
                }
            }

            @Override
            public void setEthernetIndicators(IconState iconState) {

            }

            @Override
            public void setIsAirplaneMode(IconState iconState) {

            }

            @Override
            public void setMobileDataEnabled(boolean enabled) {

            }

            @Override
            public void setConnectivityStatus(boolean noDefaultNetwork, boolean noValidatedNetwork,
                                              boolean noNetworksAvailable) {

            }

            @Override
            public void setCallIndicator(IconState iconState, int subId) {

            }
        });
        screenAdaption();
    }

    private void screenAdaption() {
        if (Utilities.isLongStripScreen(getContext()) || getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            container.setBackgroundResource(R.drawable.bg_round_corner_selector_small);
        }
    }


}
